/**
 * Created by joyi on 2017/2/16.
 */
public class SortNumbers {
    public static void main(String[] args) {
        int[] beforeSort={7,2,9,3,5,1,6,8,4};
        int[] afterSort=quickSort(beforeSort,0,8);
        System.out.println(afterSort);
        for (int a:afterSort) {
            System.out.print(a);
        }
    }

    /**
     * 快速排序算法
     * @param numbers 排序数组
     * @param start 开始下标
     * @param end 结束下标
     */
    public static int[] quickSort(int[] numbers, int start, int end) {
        if (start < end) {
            int base = numbers[start]; // 选定的基准值（第一个数值作为基准值）
            int temp; // 记录临时中间值
            int i = start, j = end;
            do {
                while ((numbers[i] < base) && (i < end))
                    i++;
                while ((numbers[j] > base) && (j > start))
                    j--;
                if (i <= j) {
                    temp = numbers[i];
                    numbers[i] = numbers[j];
                    numbers[j] = temp;
                    i++;
                    j--;
                }
            } while (i <= j);
            if (start < j)
                quickSort(numbers, start, j);
            if (end > i)
                quickSort(numbers, i, end);
        }
        return numbers;
    }
}
